package com.hotel.service.impl;

import com.hotel.config.Scheduler;
import com.hotel.mapper.TemporaryMapper;
import com.hotel.pojo.Temporary;
import com.hotel.service.TemporaryService;
import com.hotel.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TemporaryServiceImpl implements TemporaryService {
    @Autowired
    private TemporaryMapper temporaryMapper;

    @Override
    public void addTemporary(Temporary temporary) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        temporary.setReserve(dateFormat.format(date));
        temporaryMapper.addTemporary(temporary);
    }

    @Override
    public PageResult temporaryList(PageResult pageResult) {
        String userName = pageResult.getUserName();
        Long total = temporaryMapper.getTotal(userName);
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum() - 1) * size;
        String query = pageResult.getQuery();
        List<Temporary> rows = temporaryMapper.temporaryList(start,size,query,userName);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void deleteTemporary(Integer id) {
        temporaryMapper.deleteTemporary(id);
        temporaryMapper.updateStatus(id);
    }

    @Override
    public List<Temporary> findAll() {
        return temporaryMapper.findAll();
    }
}
